﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using ELGGAPI.DomainModels;
using ELGGAPI.Models;
using ELGGAPI.Data;

namespace ELGGAPI.Controllers
{
    public class GamesController : ApiController
    {
        public Response<Textclass> GetPalyGame(string gameid, string access_token)
        {
            var response = new Response<Textclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                int GameId = 0;
                int.TryParse(gameid, out GameId);
                if (GameId > 0)
                {
                    response.Data = GamesDomainModles.PlayGame(GameId);
                    response.Message = null;
                }
                else
                {
                    response.Data = null;
                    response.Message = "Invalid game id";
                }

                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<IEnumerable<GamesModel>> GetGameList(string access_token)
        {
            var response = new Response<IEnumerable<GamesModel>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }

                IEnumerable<GamesModel> games = GamesDomainModles.GetGamesList();
                response.Data = games;
                response.Message = null;


                //Error
                //response.Data = null;
                //response.Message = "Error";

                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<Idclass> PostAddGame(Request<GamesModel> request)
        {
            var response = new Response<Idclass>();
            try
            {

                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }

                if (request.Data.provider_id == null || request.Data.provider_id <= 0)
                {
                    request.Data.provider_id = user.id;
                }
                if (request.Data.provider_id == null) // if still null / could not provider Id through Authentication Token
                {
                    response.Data = null;
                    response.Message = "Game provider id not supplied";
                    return response;
                }

                response.Data = GamesDomainModles.AddGame(request.Data);


                response.Message = null;
                return response;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }

    }
}